package com.rnd.kx.mvp.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rnd.kx.mvp.R;
import com.rnd.kx.mvp.bean.Product;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

import javax.crypto.Cipher;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GGAdapter extends RecyclerView.Adapter<GGAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<Product> list;

    public OnItemClickListener mOnItemClickListener;

    public GGAdapter(Context context, List<Product> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_gg, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.layout_type.setTag(i);
        viewHolder.layout_size.setTag(i);
        viewHolder.iv_sub.setTag(i);
        viewHolder.iv_add.setTag(i);
        viewHolder.checkbox1.setTag(i);
        viewHolder.checkbox2.setTag(i);
        viewHolder.checkbox3.setTag(i);

        Product product = list.get(i);
        if (product.getPro_name() != null && !TextUtils.isEmpty(product.getPro_name())){
            viewHolder.tv_type.setText(product.getPro_name());
        }else {
            viewHolder.tv_type.setText("灌气");
        }
        if (product.getWeight() != null && !TextUtils.isEmpty(product.getWeight())){
            viewHolder.tv_size.setText(product.getWeight()+"KG");
        }else {
            viewHolder.tv_size.setText("规格");
        }
        if (product.getStatus() != null && !TextUtils.isEmpty(product.getStatus())){
            viewHolder.num.setText(product.getStatus());
        }else {
            viewHolder.num.setText("0");
        }
        if (product.getIs_buy_new().equals("1")){
            viewHolder.checkbox1.setChecked(true);
        }else if (product.getIs_buy_new().equals("0")){
            viewHolder.checkbox1.setChecked(false);
        }
        if (product.getIs_change_new().equals("1")){
            viewHolder.checkbox2.setChecked(true);
        }else if (product.getIs_change_new().equals("0")){
            viewHolder.checkbox2.setChecked(false);
        }
        if (product.getIs_annual_inspection().equals("1")){
            viewHolder.checkbox3.setChecked(true);
        }else if (product.getIs_annual_inspection().equals("0")){
            viewHolder.checkbox3.setChecked(false);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        int[] location = new int[2];
        if (mOnItemClickListener != null){
            switch (v.getId()){
                case R.id.layout_type:
                    v.getLocationOnScreen(location);
                    mOnItemClickListener.onClick(v,ViewName.LAYOUT_TYPE,position,location);
                    break;
                case R.id.layout_size:
                    v.getLocationOnScreen(location);
                    mOnItemClickListener.onClick(v,ViewName.LAYOUT_SIZE,position, location);
                    break;
                case R.id.iv_sub:
                    mOnItemClickListener.onClick(v,ViewName.IV_SUB,position, location);
                    break;
                case R.id.iv_add:
                    mOnItemClickListener.onClick(v,ViewName.IV_ADD,position, location);
                    break;
                case R.id.chebox1:
                    mOnItemClickListener.onClick(v,ViewName.CHECKBOX1,position, location);
                    break;
                case R.id.chebox2:
                    mOnItemClickListener.onClick(v,ViewName.CHECKBOX2,position, location);
                    break;
                case R.id.checkbox3:
                    mOnItemClickListener.onClick(v,ViewName.CHECKBOX3,position, location);
                    break;
            }
        }
    }

    public void updateData(ArrayList<Product> products) {
        this.list = products;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img)
        ImageView img;
        @BindView(R.id.layout_type)
        RelativeLayout layout_type;
        @BindView(R.id.tv_type)
        TextView tv_type;
        @BindView(R.id.layout_size)
        RelativeLayout layout_size;
        @BindView(R.id.tv_size)
        TextView tv_size;
        @BindView(R.id.iv_sub)
        TextView iv_sub;
        @BindView(R.id.iv_add)
        TextView iv_add;
        @BindView(R.id.num)
        TextView num;
        @BindView(R.id.chebox1)
        CheckBox checkbox1;
        @BindView(R.id.chebox2)
        CheckBox checkbox2;
        @BindView(R.id.checkbox3)
        CheckBox checkbox3;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            layout_type.setOnClickListener(GGAdapter.this);
            layout_size.setOnClickListener(GGAdapter.this);
            iv_sub.setOnClickListener(GGAdapter.this);
            iv_add.setOnClickListener(GGAdapter.this);
            checkbox1.setOnClickListener(GGAdapter.this);
            checkbox2.setOnClickListener(GGAdapter.this);
            checkbox3.setOnClickListener(GGAdapter.this);

        }
    }

    public interface OnItemClickListener {
        void onClick(View view, ViewName viewName, int position, int[] location);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener=onItemClickListener;
    }

    public enum ViewName{
        LAYOUT_TYPE,
        LAYOUT_SIZE,
        IV_SUB,
        IV_ADD,
        CHECKBOX1,
        CHECKBOX2,
        CHECKBOX3
    }
}
